//
//  LookViewController.m
//  滴滴快拍
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LookViewController.h"
#import <BmobSDK/Bmob.h>
#import "AGLChannel.h"
#import "LookViewCell.h"
#import "Header.h"


@interface LookViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
LookViewCellDelegate
>

@property (nonatomic, strong)NSMutableArray *lookArr;
@property (nonatomic, strong)BaseTableView *tableView;
@property (nonatomic, strong)NSDateFormatter *dateFormatter;


@end

@implementation LookViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"我上传的";
    
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64 -49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = SCREEN_HEIGHT /2.5;
    
    //    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
    
    _dateFormatter = [[NSDateFormatter alloc] init];
    [_dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    [_dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    [self createData];

}



- (void)createData {
    
    //创建BmobQuery实例，指定对应要操作的数据表名称
    BmobQuery *query = [BmobQuery queryWithClassName:@"VideoTable"];
    //按updatedAt进行降序排列
    [query orderByDescending:@"updatedAt"];
    //返回最多20个结果
    query.limit = 100;
    //执行查询
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        //处理查询结果
        self.lookArr = [NSMutableArray array];
        for (BmobObject *obj in array) {
            AGLChannel *info    = [[AGLChannel alloc] init];
            if ([obj objectForKey:@"ft"]) {
                info.ft    = [obj objectForKey:@"ft"];
            }
            if ([obj objectForKey:@"streambase"]) {
                info.streambase  = [obj objectForKey:@"streambase"];
            }
            if ([obj objectForKey:@"previewStr"]) {
                info.previewStr = [obj objectForKey:@"previewStr"];
            }
            if ([obj objectForKey:@"objectId"]) {
                info.objectId = [obj objectForKey:@"objectId"];
            }
            info.finishTimeNice     = [_dateFormatter stringFromDate:obj.updatedAt];
            
            [self.lookArr addObject:info];
        }
        
        [_tableView reloadData];
    }];
    
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.lookArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class]) forIndexPath:indexPath];
    //
    static NSString *cellIdentifier = @"Cell";
    
    LookViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(cell == nil) {
        cell = [[LookViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        UIImageView *lineImageView = [[UIImageView alloc] init];
        lineImageView.frame        = CGRectMake(0, 114, 320, 1);
        lineImageView.image        = [UIImage imageNamed:@"line"];
        [cell.contentView addSubview:lineImageView];
    }
    
    AGLChannel *info             = (AGLChannel*)[self.lookArr objectAtIndex:indexPath.row];
    cell.model = info;
    cell.index = indexPath.row;
    cell.delegate = self;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AGLChannel *channel = self.lookArr[indexPath.row];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:channel.streambase]];
    
}

- (void)deleteCellWithObjectId:(NSString *)objectId ft:(NSString *)ft index:(NSInteger)index {
    //创建一个提示框页面
    //UIAlertControllerStyleAlert 在中间弹出
    //UIAlertControllerStyleActionSheet z在底下弹出
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:[NSString stringWithFormat:@"您确定删除%@吗?", ft] preferredStyle:UIAlertControllerStyleAlert];
    
    //出示提示框页面
    //参数1.设置弹出页面的对象
    //参数2.是否有动画
    //参数3.block 板块 弹出完成时的执行block
    [self presentViewController:alertC animated:YES completion:^{
        //        NSLog(@"页面跳转完成");
    }];
    
    //添加一个确定按钮
    UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //        //删除数据
        //        [[DataBaseManager shareFMDB] deleteDownloadedWithTitle:title];
        
        BmobQuery *bquerydelete = [BmobQuery queryWithClassName:@"VideoTable"];
        [bquerydelete getObjectInBackgroundWithId:objectId block:^(BmobObject *object, NSError *error){
            if (error) {
                //进行错误处理
            }
            else{
                if (object) {
                    //异步删除object
                    [object deleteInBackground];
                }
            }
            //重新获取数组
            //刷新
            //[self createData];
            //要实现删除,第一件事就是删掉对应数组里数据
            [self.lookArr removeObjectAtIndex:index];
            [self.tableView reloadData];
            
        }];
        
        
    }];
    UIAlertAction *alertB = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    
    
    //添加提示按钮
    [alertC addAction:alertB];
    [alertC addAction:alertA];
    

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
